// bookgo/borrow/choose/addcard/addcard.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        reader_id:'',
        password:'',
        casArray: ['请选择图书馆','武汉市洪山区图书馆', '武汉市江夏区图书馆', '湖北省黄石市图书馆'],
        casIndex: 0,
        aid:'',
        title:'',
        name:'',
        pid:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    bindStuidInput: function (e) {
        this.setData({
            reader_id: e.detail.value
        });

    },
    bindPasswordInput: function (e) {
        this.setData({
            password: e.detail.value
        });        
    },


//选择图书馆（为了获取图书馆aid）
    bindCasPickerChange: function (e) {
        var that = this;
        this.setData({
            casIndex: e.detail.value
        })
        console.log('选的是', this.data.casArray[e.detail.value])
        var title = this.data.casArray[e.detail.value];
            that.setData({
                title:title
            });
        
        wx.request({
            url: 'https://2016.bookgo.com.cn/wxapp/login/choose_aid',
            data:{
                title:title
            },
            success: function (res) {
                console.log('--------------bindCasPickerChange--->success--->res-----------');
                console.log(res);
                that.setData({
                    aid:res.data.msg.aid
                })
            }
        })        
    },

    //表单提交事件
    formSubmit: function (e) {
        console.log(e);
        var that = this;
        
        console.log(that.data.aid);
        console.log(that.data.reader_id);
        console.log(that.data.password);
        wx.request({
            url: 'https://2016.bookgo.com.cn/wxapp/choose/check',
            data: {
                aid:that.data.aid,
                reader_id:that.data.reader_id,
                password:that.data.password
            },
            success: function (res) {
                console.log('-----choose/check------->success-->res---------');
                console.log(res);
                if(res.data.code == 101){
                    that.setData({
                        name:res.data.data.name
                    })
                    that.add_card();
                }
            }
        })

    },
    add_card: function() {
        var that = this;
        var openid = wx.getStorageSync('openid');
        console.log('------------打印的值------------');
        console.log(that.data.reader_id);
        console.log(that.data.password);
        console.log(that.data.aid);
        console.log(that.data.name);
        console.log(that.data.pid);
        console.log(that.data.title);
        console.log(openid);
        console.log('------------打印的值------------');
        wx.request({
            url: 'https://2016.bookgo.com.cn/wxapp/choose/add_card',
            data: {
                aid: that.data.aid,
                reader_id: that.data.reader_id,
                password: that.data.password,
                title: that.data.title,
                openid: openid,
                name: that.data.name,
                pid: that.data.pid
            },
            method:'post',
            success: function (res) {
                console.log(res);
                wx.redirectTo({
                    url: '/bookgo/borrow/choose/choose?pid=' + that.data.pid,
                });
            }
        })

    },

    onLoad: function (options) {
        console.log('-------addcard--->onLoad--------');
        console.log(options);
        var pid = options.pid;
        this.setData({
            pid:pid
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
    
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
    
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
    
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
    
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
    
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
    
    }
})